<template>
  <div id="login_box">
    <div class="tit">
      <span>登录 | LOGIN</span>
    </div>
    <div class="login_form">
      <el-form
        :model="loginInfo"
        autoComplete="on"
        status-icon
        :rules="rules"
        ref="loginInfo"
      >
        <div class="username">
          <img
            src="../assets/img/icon_username.png"
            width="18"
            height="18"
            style="margin-top: 15px"
          />
          <input
            type="text"
            placeholder="请输入用户名"
            v-model="loginInfo.loginName"
          />
          <!--<el-input name="loginName" type="text" v-model="loginInfo.loginName" autoComplete="on" placeholder="用户登录名" />-->
        </div>
        <div class="psw">
          <img
            src="../assets/img/icon_password.png"
            width="18"
            height="18"
            style="margin-top: 15px"
          />
          <input
            type="password"
            placeholder="请输入用户密码"
            v-model="loginInfo.password"
          />
          <!--<el-input name="password" :type="passwordType" @keyup.enter.native="submitForm" v-model="loginInfo.password" autoComplete="on" placeholder="密码" />-->
        </div>

        <!--<el-form-item prop="codeValidate">-->
        <!--<el-row :gutter="20">-->
        <!--<el-col :span="16">-->
        <!--<el-input type="type" v-model="loginInfo.codeValidate" autocomplete="off" placeholder="请输入验证码">-->
        <!--<template slot="prepend"><i class="fa fa-th-large"></i></template>-->
        <!--</el-input>-->
        <!--</el-col>-->
        <!--<el-col :span="8"><img src="" width="100%" alt="验证码"/></el-col>-->
        <!--</el-row>-->
        <!--</el-form-item>-->
        <el-form-item
          class="form-checkbox-item"
          style="text-align: right; margin-top: 20px"
        >
          <el-checkbox
            label="记住密码"
            v-model="loginInfo.isChecked"
            @change="savePwd"
          ></el-checkbox>
        </el-form-item>
        <el-form-item style="margin-top: 70px">
          <el-button round @click="submitForm()" class="login_btn"
          >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import MD5 from "js-md5";
import { mapActions } from "vuex";
import LoginApi from "@/api/system/login";
import {
  setCookies,
  getCookies,
  removeCookies,
  setStorage
} from "@/utils/auth";
import * as stn from "@/constants/storeName";
export default {
  data() {
    return {
      loginInfo: {
        loginName: "",
        password: "",
        codeValidate: "",
        isChecked: false
      },
      passwordType: "password",
      rules: {
        loginName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    ...mapActions({
      setToken: "setToken"
    }),
    keyDown(e) {
      //如果是回车则执行登录方法
      if (e.keyCode == 13) {
        this.submitForm();
      }
    },
    savePwd() {
      if (this.loginInfo.isChecked) {
        let md5pwd = "";
        // 判断密码是否已md5
        if (this.loginInfo.password.indexOf("SHA256_") > -1) {
          md5pwd = this.loginInfo.password;
        } else {
          md5pwd = MD5(this.loginInfo.password).toUpperCase();
          md5pwd = "SHA256_" + md5pwd;
        }
        setCookies("userName", this.loginInfo.loginName);
        setCookies("userPass", md5pwd);
      } else {
        removeCookies("userName");
        removeCookies("userPass");
      }
    },
    submitForm() {
      if (!this.loginInfo.loginName) {
        this.$message({
          message: "请输入用户名",
          type: "error"
        });
        return;
      }
      if (!this.loginInfo.password) {
        this.$message({
          message: "请输入密码",
          type: "error"
        });
        return;
      }

      /*let md5pwd = "";
      // 判断密码是否已md5
      if (this.loginInfo.password.indexOf("SHA256_") > -1) {
        md5pwd = this.loginInfo.password.split("SHA256_")[1];
        //console.log(md5pwd,33333)
      } else {
        md5pwd = MD5(this.loginInfo.password).toUpperCase();
      }*/
      let data = {
        loginName: this.loginInfo.loginName,
        password: this.loginInfo.password
      };
      LoginApi.loginByUsername(data).then(res => {
        let data = res.data;
        let dataMune = data.menuList;
        console.table("dataMune", dataMune);
        localStorage.setItem("muneQA", "1");
        for (let i = 0; i < dataMune.length; i++) {
          if (dataMune[i].name == "报告管理") {
            localStorage.setItem("muneQA", "2");
          }
        }
        this.savePwd();
        localStorage.setItem("isToken", data.token);
        this.setToken(data.token);
        setStorage(stn.MENU_LIST, data.menuList);
        setStorage(stn.USER_RELATION, data.userRelation);
        //setStorage(stn.MENUS_PERMISSION, data.menusPermission)
        sessionStorage.setItem("isNotify", "false");
        this.$router.push("/");
      });
    }
  },
  created() {
    let userName = getCookies("userName");
    let userPass = getCookies("userPass");
    if (userName && userPass) {
      this.loginInfo.loginName = userName;
      this.loginInfo.password = userPass;
      this.loginInfo.isChecked = true;
    } else {
      return;
    }
  },
  mounted() {
    //绑定事件
    window.addEventListener("keydown", this.keyDown);
  },
  destroyed() {
    window.removeEventListener("keydown", this.keyDown, false);
  }
};
</script>

<style scope lang="scss" src="../assets/css/components/loginForm.scss"></style>
<style scope lang="scss" src="../../theme/checkbox.css"></style>
<style scoped>
.username {
  width: 360px;
  height: 50px;
  border-radius: 25px;
  background-color: white;
  border: #eeeeee solid 1px;
  font-size: 16px;
  color: #b2b2b2;
  display: flex;
  justify-content: space-around;
}
.username input,
.psw input {
  width: 300px;
  height: 42px;
  border: none;
  border-radius: 25px;
  margin-top: 3px;
  color: #b2b2b2;
  font-size: 16px;
  outline: none;
}

.psw {
  width: 360px;
  height: 50px;
  border-radius: 25px;
  background-color: white;
  margin-top: 25px;
  font-size: 16px;
  color: #b2b2b2;
  display: flex;
  justify-content: space-around;
  border: #eeeeee solid 1px;
}
.login_btn {
  width: 100%;
  border-color: #49ad18;
  color: #000;
}
.login_btn:hover {
  border-color: #49ad18;
  background-color: #49ad18;
  color: #fff;
}
</style>
